package org.apache.dto;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.model.Cookie;
import org.springframework.util.StringUtils;

public class Cookies {

    private List<Cookie> cookies;
    private String cookie;
    private Map<String, String> cookieMap = new HashMap<String, String>();

    public List<Cookie> getCookies() {

        return cookies;
    }

    public void setCookies(List<Cookie> cookies) {

        this.cookies = cookies;
        cookieMap = putNewCookies(cookies);
    }

    public String getCookie() {

        cookie = "";
        for (Map.Entry<String, String> entry : cookieMap.entrySet()) {
            cookie += ";" + entry.getKey() + "=" + entry.getValue();
        }
        if (!StringUtils.isEmpty(cookie))
        {
            return cookie.substring(1);
        }
        return "";
    }

    public void setCookie(String cookie) {

        this.cookie = cookie;
    }

    public Map<String, String> getCookieMap() {

        return cookieMap;
    }

    public void setCookieMap(Map<String, String> cookieMap) {

        this.cookieMap = cookieMap;
    }

    public Map<String, String> putNewCookies(Map<String, String> cookieMapNew) {

        for (Map.Entry<String, String> entry : cookieMapNew.entrySet()) {
            cookieMap.put(entry.getKey(), entry.getValue());
        }
        return cookieMap;
    }

    public Map<String, String> putNewCookies(List<Cookie> cookieLst) {

        if (cookieLst != null && cookieLst.size() > 0) {
            for (Cookie c : cookieLst) {
                cookieMap.put(c.getName(), c.getValue());
            }
        }
        return cookieMap;
    }

}
